﻿
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using Ymt.Repository;
using YmtRpc.Domain;
using YmtRpc.Domain.Enum;
using Ymatou.CommonService;

namespace YmtRpc.Web.Admin.Controllers
{
    /// <summary>
    /// 服务元数据的查询、修改
    /// </summary>
    [Authorize]
    public class ServiceMetadataController : Controller
    {
        [AllowAnonymous]
        public ActionResult Index(string appId)
        {
            ViewBag.AppList = RepositoryHub.ServiceMetadataRepository.GetAppList();
            ViewBag.AppId = appId;

            List<ServiceMetadata> list = RepositoryHub.ServiceMetadataRepository.GetList(appId);

            return View(list);
        }

        public ActionResult Edit(string appId, string apiName, string httpMethod)
        {
            ServiceMetadata serviceMetadata = RepositoryHub.ServiceMetadataRepository.GetServiceMetadata(appId, apiName, httpMethod);

            return View(serviceMetadata);
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(FormCollection collection)
        {
            string appId = collection["AppId"];
            string apiName = collection["ApiName"];
            string httpMethod = collection["HttpMethod"];
            string Host = collection["Host"];
            string ApiPath = collection["ApiPath"];
            string Description = collection["Description"];
            string Owner = collection["Owner"];

            if (string.IsNullOrEmpty(appId)) throw new Exception("appId不能为空");
            if (string.IsNullOrEmpty(apiName)) throw new Exception("apiName不能为空");
            if (string.IsNullOrEmpty(httpMethod)) throw new Exception("httpMethod不能为空");

            DateTime updateTime = DateTime.Now;
            ServiceMetadata serviceMetadata = RepositoryHub.ServiceMetadataRepository.GetServiceMetadata(appId, apiName, httpMethod);

            bool updateResult = false;
            if (serviceMetadata == null)
            {
                serviceMetadata = new ServiceMetadata()
                {
                    AppId = appId,
                    ApiName = apiName,
                    HttpMethod = httpMethod,
                    Host = Host,
                    ApiPath = ApiPath,
                    Description = Description,
                    Owner = Owner,
                    CreateTime = updateTime,
                    LastUpdateTime = updateTime,
                    LastUpdateTicks = updateTime.Ticks
                };

                updateResult = RepositoryHub.ServiceMetadataRepository.TryInsert(serviceMetadata);
            }
            else
            {
                serviceMetadata = new ServiceMetadata()
                {
                    AppId = appId,
                    ApiName = apiName,
                    HttpMethod = httpMethod,
                    Host = Host,
                    ApiPath = ApiPath,
                    Description = Description,
                    Owner = Owner,
                    LastUpdateTime = updateTime,
                    LastUpdateTicks = updateTime.Ticks
                };

                updateResult = RepositoryHub.ServiceMetadataRepository.TryUpdate(serviceMetadata);
            }

            ViewBag.Result = updateResult;

            return View();
        }

        [HttpGet]
        public JsonResult Delete(string appId, string apiName, string httpMethod)
        {
            bool deleteResult = RepositoryHub.ServiceMetadataRepository.Delete(appId, apiName, httpMethod);

            return Json(deleteResult, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Edit(FormCollection collection)
        {
            string appId = collection["AppId"];
            string apiName = collection["ApiName"];
            string httpMethod = collection["HttpMethod"];

            string Host = collection["Host"];
            string ApiPath = collection["ApiPath"];
            string Description = collection["Description"];
            string Owner = collection["Owner"];

            DateTime updateTime = DateTime.Now;

            ServiceMetadata serviceMetadata = new ServiceMetadata()
            {
                AppId = appId,
                ApiName = apiName,
                HttpMethod = httpMethod,
                Host = Host,
                ApiPath = ApiPath,
                Description = Description,
                Owner = Owner,
                LastUpdateTime = updateTime,
                LastUpdateTicks = updateTime.Ticks
            };


            bool updateResult = RepositoryHub.ServiceMetadataRepository.TryUpdate(serviceMetadata);
            serviceMetadata = RepositoryHub.ServiceMetadataRepository.GetServiceMetadata(appId, apiName, httpMethod);

            ViewBag.Result = updateResult;

            return View(serviceMetadata);
        }
    }
}
